import fetch from "node-fetch";
import FormData from 'form-data';////需要自己安装
import fs from 'fs';
import sharp from 'sharp';
import amesu from "./amesu";
export async function sendImage(msgId: string, channel_id: string, picName: string,) {
  let picId = Date.now();
  let picData = fs.readFileSync(picName);
  const compressedImageBuffer = await sharp(picData)
    .jpeg({ quality: 90 })
    .toBuffer();

  // let picData = fs.createReadStream(compressedImageBuffer );
  let formdata = new FormData();
  formdata.append("msg_id", msgId);
  //formdata.append("content", "123456");
  formdata.append("file_image", compressedImageBuffer, { filename: 'compressed.jpg' });
  await fetch(`https://api.sgroup.qq.com/channels/${channel_id}/messages`, {
    method: "POST",
    headers: {
      ...formdata.getHeaders(),
      "Authorization": `Bot ${amesu.getBotConfig().appid}.${amesu.getBotConfig().token}`
    },
    body: formdata
  }).then(async res => {
    const body = await res.json();
    if (body) {
      console.log('img send success');
    }
    // picData.close(); // 关闭流对象
  }).catch(error => {
    console.error('img send error', error);
    // picData.close(); // 关闭流对象
  });
}